Le chunk suivant contient les packages utilisés pour produire des cartes avec des shapefiles, et des données que l’on peut représenter dans l’espace.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(sf)
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(leaflet)
france <- st_read(paste0(getwd(),"/data/france/traitcote.shp"),crs=4326)
## Reading layer `traitcote' from data source
## `C:\Users\rlecuyer\Desktop\Carto\data\france\traitcote.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 602 features and 3 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -9.2996 ymin: 40.08594 xmax: 8.232199 ymax: 51.19549
## Geodetic CRS: WGS 84
A quoi ressemble ce shapefile ?
ggplot()+
geom_sf(data=france,fill="darkorange4")
Exemple : prenons ce magnifique classement des 10 plus beaux coins de pêche en france
(source : https://petitsfrenchies.com/les-10-plus-beaux-endroits-pour-pecher-en-france/)
## Lieu Latitude Longitude
## 1 Sioule – Allié 46.31218 3.297298
## 2 La Beaume – Ardèche 44.48011 4.253149
## 3 Le Lac de l’Abbaye – Jura 46.52948 5.910707
## 4 Le Cousin à Avallon – Yonne 47.48173 3.908853
## 5 Le Lac du Bourget – Savoie 45.73091 5.860195
## 6 Quiberon – Morbihan 47.47771 -3.119289
Comment choisir notre prochaine destination pêche ?
ggplot()+
geom_sf(data=france,fill="darkorange4")+
geom_point(data=data_peche %>%
filter(!str_detect(Lieu,"union")),aes(x=Longitude,y=Latitude))+
geom_label(data=data_peche %>%
filter(!str_detect(Lieu,"union")),aes(x=Longitude,y=Latitude+0.5,label=Lieu),size=2)
Et en carte “dynamique” ?
require(plotly)
## Le chargement a nécessité le package : plotly
##
## Attachement du package : 'plotly'
## L'objet suivant est masqué depuis 'package:ggplot2':
##
## last_plot
## L'objet suivant est masqué depuis 'package:stats':
##
## filter
## L'objet suivant est masqué depuis 'package:graphics':
##
## layout
p <- ggplot()+
geom_sf(data=france,fill="darkorange4")+
geom_point(data=data_peche %>%
filter(!str_detect(Lieu,"union")),
aes(x=Longitude,y=Latitude,label=Lieu))
## Warning in geom_point(data = data_peche %>% filter(!str_detect(Lieu, "union")),
## : Ignoring unknown aesthetics: label
p <- ggplotly(p)
p
library(maps)
##
## Attachement du package : 'maps'
## L'objet suivant est masqué depuis 'package:purrr':
##
## map
mapStates = map("world", fill = TRUE, plot = FALSE)
leaflet(data = data_peche) %>% addTiles(urlTemplate = "https://mts1.google.com/vt/lyrs=s&hl=en&src=app&x={x}&y={y}&z={z}&s=G", attribution = 'Google') %>% addCircleMarkers(popup = ~Lieu)
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
leaflet(data = data_peche) %>%
addTiles(urlTemplate = "https://mts1.google.com/vt/lyrs=s&hl=en&src=app&x={x}&y={y}&z={z}&s=G", attribution = 'Google') %>%
setView(-1,47,5) %>%
addCircleMarkers(popup = ~Lieu)
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively